package com.sangeng.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.CategoryListVo;
import com.sangeng.domain.vo.ExcelCategoryVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.util.List;


@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有的分类
     * @return
     */
    @GetMapping("/listAllCategory")
    public ResponseResult<CategoryListVo> listAllCategory(){

        return categoryService.listAllCategory();
    }

    /**
     * 对分类数据进行导出功能
     * 但是需要对功能进行权限校验
     * @param response
     */
    @GetMapping("/export")
    //用自定义的权限校验方法
    @PreAuthorize("@ps.hasPermission('content:category:export')")
    public void export(HttpServletResponse response){
        try {
            //设置下载文件的请求头
            WebUtils.setDownLoadHeader(SystemConstants.CATEGORY_FILE_NAME,response);
            //获取需要导出的数据
            List<Category> categoryVos = categoryService.list();

            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class)
                    .autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    /**
     * 分类分页查询
     * 需要分页查询分类列表。能根据分类名称进行模糊查询。
       能根据状态进行查询。
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<PageVo> getAllCategory(Integer pageNum,Integer pageSize,String name,String status){
        return categoryService.getAllCategory(pageNum,pageSize,name,status);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public ResponseResult saveData(@RequestBody Category category){
        return categoryService.saveData(category);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<Category> getDataById(@PathVariable Long id){
        return categoryService.getDataById(id);
    }

    /**
     * 更新数据
     * @param category
     * @return
     */
    @PutMapping
    public ResponseResult updateData(@RequestBody Category category){
        return categoryService.updateData(category);
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteDataById(@PathVariable Long id){
        return categoryService.deleteDataById(id);
    }
}
